package Simulator.MdtGUI;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Shared.AmbState;
import Shared.Utils;
import Simulator.MDT;

public class MDTGUILoginPanel extends JPanel {

	private CardLayout layout;
	private MDTGUILoginPanel ref;
	
	private JTextField crewNameField;
	private JPasswordField passwordField;
	private JTextField crewIDField;
	private JComboBox typeList;
	private MDT mdt;
	private MDTScreen parent;
	private JButton registerButton,loginButton,unregisterButton, clearErrorsButton;
	
	public MDTGUILoginPanel(MDT mdt, MDTScreen parent)
	{	
		this.parent = parent;
		this.mdt = mdt;
		layout = new CardLayout();
		this.setLayout(layout);
		
		this.add("main", new MainPanel());
		this.add("login", new LoginPanel());
		this.add("register", new RegisterPanel());
		
		ref = this;
		
		this.refreshButtons();
	}
	
	class MainPanel extends JPanel {
		public MainPanel()
		{
			this.setLayout(new GridBagLayout ());
			
			loginButton = new JButton("Log in");
	        loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
	        this.add(loginButton,new GridBagConstraints (0, 0, 1, 1, 0, 0,
	                GridBagConstraints.CENTER,
	                GridBagConstraints.CENTER,
	                new Insets (10,10,10,10), 0, 0));
	        loginButton.addActionListener(new LogginListener());
	        
	        registerButton = new JButton("Register this ambulance");
	        registerButton.setAlignmentX(Component.CENTER_ALIGNMENT);
	        this.add(registerButton,new GridBagConstraints (0,1, 1, 1, 0, 0,
	                GridBagConstraints.CENTER,
	                GridBagConstraints.CENTER,
	                new Insets (10,10,10,10), 0, 0));
	        registerButton.addActionListener(new SubmitRegistrationListener());

	        unregisterButton = new JButton("Remove this ambulance from the system");
	        unregisterButton.setAlignmentX(Component.CENTER_ALIGNMENT);
	        this.add(unregisterButton,new GridBagConstraints (0,2, 1, 1, 0, 0,
	                GridBagConstraints.CENTER,
	                GridBagConstraints.CENTER,
	                new Insets (10,10,10,10), 0, 0));
	        unregisterButton.addActionListener(new UnregisterListener());
	        
	        clearErrorsButton = new JButton("Recover from crash or errors");
	        clearErrorsButton.setAlignmentX(Component.CENTER_ALIGNMENT);
	        this.add(clearErrorsButton,new GridBagConstraints (0,3, 1, 1, 0, 0,
	                GridBagConstraints.CENTER,
	                GridBagConstraints.CENTER,
	                new Insets (10,10,10,10), 0, 0));
	        clearErrorsButton.addActionListener(new ClearErrorsListener());
	        
			refreshButtons();

		}
	}
	
	public void refreshButtons()
	{
		if(mdt!=null)
		{
			AmbState status = mdt.getAmbulanceStatus();
			switch(status)
			{
			
			case UNREGISTERED:
				loginButton.setEnabled(false);
				registerButton.setEnabled(true);
				unregisterButton.setEnabled(false);
				clearErrorsButton.setEnabled(false);
				break;
			case INACTIVE: // == logout
				loginButton.setEnabled(true);
				registerButton.setEnabled(false);
				unregisterButton.setEnabled(true);
				clearErrorsButton.setEnabled(false);
				break;
			case ERROR: // == logout
				loginButton.setEnabled(false);
				registerButton.setEnabled(false);
				unregisterButton.setEnabled(false);
				clearErrorsButton.setEnabled(true);
				break;
			default :
				loginButton.setEnabled(false);
				registerButton.setEnabled(false);
				unregisterButton.setEnabled(false);
				clearErrorsButton.setEnabled(false);
				break;
			}
		}
		else
		{
			loginButton.setEnabled(false);
			registerButton.setEnabled(false);
			unregisterButton.setEnabled(false);
		}
	}
	
	class LoginPanel extends JPanel {
		
		public LoginPanel()
		{
			
			this.setLayout(new GridBagLayout ());
			

			JLabel crewName = new JLabel("Crew name :");
			
			crewNameField = new JTextField(20);
			crewNameField.setMaximumSize(crewNameField.getPreferredSize());
			//crewNameField.setAlignmentX(Component.CENTER_ALIGNMENT);
	        
			Box hBox1 = Box.createHorizontalBox();
	        hBox1.add(crewName);
	        hBox1.add(Box.createHorizontalStrut(15));
	        hBox1.add(crewNameField);
	        
			JLabel crewID = new JLabel("Crew ID :");
			

			crewIDField = new JTextField(20);
			crewIDField.setMaximumSize(crewNameField.getPreferredSize());
			
			Box hBox2 = Box.createHorizontalBox();
	        hBox2.add(crewID);
	        hBox2.add(Box.createHorizontalStrut(15));
	        hBox2.add(crewIDField);

	        JButton loginSubmitBut = new JButton("Login");
	        loginSubmitBut.addActionListener(new ConnectListener());
	        
	        JButton cancelBut = new JButton("Cancel");
	        cancelBut.addActionListener(new CancelListener());
	        
	        Box hBox4 = Box.createHorizontalBox();
	        hBox4.add(loginSubmitBut);
	        hBox4.add(Box.createHorizontalStrut(15));
	        hBox4.add(cancelBut);
	        
	        Box vBox = Box.createVerticalBox();
	        
	        vBox.add(hBox1);
	        vBox.add(Box.createVerticalStrut(20));
	        vBox.add(hBox2);
	        //vBox.add(Box.createVerticalStrut(20));
	        //vBox.add(hBox3);
	        vBox.add(Box.createVerticalStrut(40));
	        vBox.add(hBox4);
	        
	        JPanel conteneur = new JPanel();
	        conteneur.add(vBox,BorderLayout.CENTER);
	        conteneur.setAlignmentX(Component.CENTER_ALIGNMENT);
	        
	        this.add(conteneur,new GridBagConstraints (0,1, 1, 1, 0, 0,
	                GridBagConstraints.CENTER,
	                GridBagConstraints.CENTER,
	                new Insets (10,10,10,10), 0, 0)); 
		}
	}
		
    class LogginListener implements ActionListener {
    	
        public void actionPerformed(ActionEvent arg0) {
        	layout.show(ref, "login");
        }
	}
	
    class ConnectListener implements ActionListener {
    	
        public void actionPerformed(ActionEvent arg0) {
        	
        	    String crewName = crewNameField.getText();
        	    String crewID = crewIDField.getText();
        	    
        	    if(crewName.equals("") || crewID.equals(""))
        	    	JOptionPane.showMessageDialog(ref,"Fields cannot be empty !","Error", JOptionPane.ERROR_MESSAGE);
        	    else
        	    {
        	    	try {
	        	    	int id = Integer.parseInt(crewID); 
	        	    	
	    	    		if(mdt!=null)
	    	    			ref.mdt.login();
	    	    		else
	    	    			System.err.println("Mdt.login impossible car mdt=null");
	    	    		
	    	    		if(mdt.isLogged())
	    	    			Utils.disp("MDTGUI>> Mdt.isLogged=True");
	    	    		
	    	    		layout.show(ref,"main");
	    	    		refreshButtons();
	    	    		ref.parent.setLoggedPanel();
        	    	}
        	    	catch(NumberFormatException e)
        	    	{
        	    		JOptionPane.showMessageDialog(ref,"Crew ID must be an number !","Error", JOptionPane.ERROR_MESSAGE);
        	    	}
        	    }
        }
	}
    
	class RegisterListener implements ActionListener {
		
	    public void actionPerformed(ActionEvent e) {
	    	
	    	mdt.register();
	    	
	    	layout.show(ref, "register");  
	    	
	    }
	}
	
	class UnregisterListener implements ActionListener {
		
	    public void actionPerformed(ActionEvent e) {
	    	if(mdt!=null)
	    		mdt.unregister();
	    	else
	    		System.err.println("Mdt.unregister impossible car mdt=null");
	    	
	    	if(mdt.isLogged())
	    		System.err.println("MdtGUILoginPanel>> Unregister:: Apres la methode, le mdt est tj logged !");
	    	
	    	JOptionPane.showMessageDialog(ref,"The ambulance was removed from the software system.");
	    	refreshButtons();
	    	layout.show(ref,"main");
	    	
	    }
	}
	
	class ClearErrorsListener implements ActionListener {
		
	    public void actionPerformed(ActionEvent e) {
	    	
	    	mdt.gotRepaired();
	    	
	    	JOptionPane.showMessageDialog(ref,"The ambulance was cleared from its errors.");
	    	refreshButtons();
	    	
	    	layout.show(ref, "main");  
	    	
	    }
	}
	
	class CancelListener implements ActionListener {
		
	    public void actionPerformed(ActionEvent e) {
	    	layout.show(ref, "main");                   
	    }
	}
	
	// Classe vouee a disparaitre si on n'a besoin de rien demander a l'utilisateur pour register une nouvelle ambulance
		class RegisterPanel extends JPanel {
			
			public RegisterPanel()
			{
				this.setLayout(new GridBagLayout ());
	 
				JLabel ambType = new JLabel("Ambulance type :");
				
				String[] tmp = { "simple", "equiped" };
		        typeList = new JComboBox(tmp);
		        typeList.setSelectedIndex(0);
		        typeList.setMaximumSize(crewIDField.getPreferredSize());
		        
		        Box hBox1 = Box.createHorizontalBox();
		        hBox1.add(ambType);
		        hBox1.add(Box.createHorizontalStrut(15));
		        hBox1.add(typeList);
		
		        JButton registerSubmitBut = new JButton("Register");
		        registerSubmitBut.addActionListener(new SubmitRegistrationListener());
		        
		        JButton cancelBut = new JButton("Cancel");
		        cancelBut.addActionListener(new CancelListener());
		        
		        Box hBox2 = Box.createHorizontalBox();
		        hBox2.add(registerSubmitBut);
		        hBox2.add(Box.createHorizontalStrut(15));
		        hBox2.add(cancelBut);
		        
		        Box vBox = Box.createVerticalBox();
		        
		        vBox.add(hBox1);
		        vBox.add(Box.createVerticalStrut(20));
		        vBox.add(hBox2);
		        
		        JPanel conteneur = new JPanel();
		        conteneur.add(vBox,BorderLayout.CENTER);
		        conteneur.setAlignmentX(Component.CENTER_ALIGNMENT);
		        
		        this.add(conteneur,new GridBagConstraints (0,1, 1, 1, 0, 0,
		                GridBagConstraints.CENTER,
		                GridBagConstraints.CENTER,
		                new Insets (10,10,10,10), 0, 0));
			}
		}
	
	class SubmitRegistrationListener implements ActionListener {
		
	    public void actionPerformed(ActionEvent e) {
	    	if(mdt!=null)
	    		mdt.register();
	    	
	    	JOptionPane.showMessageDialog(ref,"The ambulance is now registered in the software system.");
	    	refreshButtons();
	    	layout.show(ref,"main");
	    	
	    	//ref.parent.setLoggedPanel(); (quand on enregistre une ambulance, on n'a pas encore acces au MDT car pas encore logged)
	    }
	}
}
